package com.jcl.core.web.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName CookiesUtils

 * @Author yyliu
 * @Date 2018/9/5 11:10
 * @Version 1.0
 **/
public class CookiesUtils {

    private static final Logger logger = LoggerFactory.getLogger(CookiesUtils.class);

    public static final String WECHAT_COOKIE_USER_INFO = "WECHAT_COOKIE_USER_INFO_";

    public static final String WECHAT_COOKIE_USER_KEY = "WECHAT_COOKIE_USER_KEY";

    /* *
     * @Author yyliu
     * @Description 获取微信cookie存放的userKey
     * @Date 11:36 2018/9/5
     * @Param [request]
     * @return javax.servlet.http.Cookie
     **/
    public static String getWechatUserKey(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = CookiesUtils.readCookieMap(request, CookiesUtils.WECHAT_COOKIE_USER_KEY);
        if (null != cookieMap) {
            if (cookieMap.containsKey(WECHAT_COOKIE_USER_KEY)) {
                Cookie cookie = cookieMap.get(WECHAT_COOKIE_USER_KEY);
                if (null != cookie) {
                    logger.info("request wechat user cookie key={}, value={}", WECHAT_COOKIE_USER_KEY, cookie.getValue());
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    /* *
     * @Author yyliu
     * @Description 存放微信userKey
     * @Date 13:18 2018/9/5
     * @Param [request, response, value]
     * @return java.lang.String
     **/
    public static String addWechatUserKey(HttpServletRequest request,
                                          HttpServletResponse response,
                                          String value) {
        addCookie(response, WECHAT_COOKIE_USER_KEY, value, request.getContextPath());
        return null;
    }

    /* *
     * @Author yyliu
     * @Description 根据条件获取cookie
     * @Date 11:27 2018/9/5
     * @Param [request, keyStr]
     * @return java.util.Map<java.lang.String,javax.servlet.http.Cookie>
     **/
    public static Map<String, Cookie> readCookieMap(HttpServletRequest request, String keyStr) {
        Map<String, Cookie> cookieMap = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                String cookieName = cookie.getName();
                if (cookieName.startsWith(keyStr)) {
                    cookieMap.put(keyStr, cookie);
                }
            }
        }
        return cookieMap;
    }

    /* *
     * @Author yyliu
     * @Description 默认添加 cookie
     * @Date 11:21 2018/9/5
     * @Param [request, response, name, value]
     * @return javax.servlet.http.Cookie
     **/
    public static Cookie addCookieDefault(HttpServletRequest request,
                                          HttpServletResponse response,
                                          String name,
                                          String value) {
        return addCookie(response, name, value, request.getContextPath());
    }

    /* *
     * @Author yyliu
     * @Description 添加cookie
     * @Date 11:21 2018/9/5
     * @Param [response, name, value, path]
     * @return javax.servlet.http.Cookie
     **/
    public static Cookie addCookie(HttpServletResponse response,
                                   String name,
                                   String value,
                                   String path) {

        Cookie cookie = new Cookie(name, value);
        if (null == path) {
            path = "/";
        }
        cookie.setPath(path);
        response.addCookie(cookie);
        if (logger.isDebugEnabled()) {
            logger.debug("new-cookie : name={}, value={}, path={}", name, value, path);
        }
        return cookie;
    }
}
